
import * as constant from '../constant'

import request from '../request'

import { push } from 'connected-react-router'

//清空数据
export const unload = () => {
    return { type: constant.USER_UNLOAD }
}

//字段同步
export const commentFieldUpdate = (key, value) => {
    return { type: constant.COMMENT_UPDATE_FIELD, key, value }
}
//创建评论
export const createComment = (slug, body) => {
    return async (dispatch) => {
        try {
            const result = await request.comment.create(slug, body)
            console.log('action comment', result);
            dispatch({ type: constant.COMMENT_CREATE_RESULT, result })  //result  : {status:0,message:result.message,errors:result.errors}
        } catch (error) { // 错误
            console.log(error);
            dispatch({ type: constant.COMMENT_CREATE_RESULT, result: { message: error.message, errors: error.errors } })
        }

    }
}
//初始化评论
export const initComments = (slug) => {
    return async (dispatch) => {
        try {
            // console.log("artions",body);
            const result = await request.comment.get(slug)
            dispatch({ type: constant.COMMENT_GET_RESULT, result })
        } catch (error) {//错误
            // console.log(error);
            dispatch({ type: constant.COMMENT_GET_RESULT, result: { status: 0, message: error.message, errors: error.errors } })
        }
    }
}
export const deleteComment = (slug, id) => {
    return async (dispatch) => {
        try {
            // console.log("artions",body);
            const result = await request.comment.delete(slug, id)
            dispatch({ type: constant.COMMENT_DELETE_RESULT, result: { ...result, id } })
        } catch (error) {//错误
            // console.log(error);
            dispatch({ type: constant.COMMENT_DELETE_RESULT, result: { status: 0, message: error.message, errors: error.errors } })
        }
    }
}



